#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from app.libs.httper import HTTP
from flask import current_app


class YuShuBook:
    # per_page = 15
    isbn_url = 'http://t.yushu.im/v2/book/isbn/{}'
    keyword_url = 'http://t.yushu.im/v2/book/search?q={}&count={}&start={}'

    def __init__(self):
        pass

    @classmethod
    def search_by_isbn(cls, isbn):
        url = cls.isbn_url.format(isbn)
        # url = self.isbn_url
        result = HTTP.get(url)
        return result

    @classmethod
    def search_by_keyword(cls, keyword, page=1):
        url = cls.keyword_url.format(keyword, current_app.config['PER_PAGE'], cls.calculate_start(page))
        result = HTTP.get(keyword)
        # 返回值为字符串类型，其他类型（dict...）报错
        return result

    @staticmethod
    def calculate_start(page):
        return (page-1) * current_app.config['PER_PAGE']
